package com.bbz.core.designpatterns.work7;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class DynamicProxy implements InvocationHandler {
    private Object target;

    public DynamicProxy(Object target){
        this.target=target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object result=method.invoke(target,args);
        after();
        return result;
    }
    public Object getProxy(){
        return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),target.getClass().getInterfaces(),this);
    }
    private void before(){
        System.out.println("******************Before********************");
    }
    private void after(){
        System.out.println("************************after**********************");
    }

    public static void main(String args[]){
        Image image=new RealImage();
        DynamicProxy dynamicProxy=new DynamicProxy(image);
        image=(Image)dynamicProxy.getProxy();
        image.display();
    }
}
